
<template>
   <div :style='extype'>
    {{currentValue}}
   </div>
</template>
 
<script>
    export default {
        props:['id','value','styles','disableds','data','isreadonly','index','isRequired','isPassword','exkey','extype','isSearch'],
        name: "input-view",
        data(){
            return {
                currentValue:this.value,
                currentValueOld:'',
                disableds2:this.disableds,
                first:'Y',
                pId:"",
                cId:""
            }
        },
        created(){
            if(this.isreadonly=="Y")
                this.disableds2=true;
            this.setData();
            
            
        },
        methods:{
            setData(){
                if(this.isSearch!="Y"){
                    if(!this.value){
                        if(this.data){
                            let ids=this.id.split(".");
                            if(ids.length>1){
                                let p=ids[0];
                                let id_=ids[1];
                                this.pId=p;
                                this.cId=id_;
                                if(this.index||this.index=='0'){
                                    try{
                                        this.currentValue=unescape(this.data[p][this.index][id_]);
                                    }catch(e){}
                                }else{
                                    try{
                                        this.currentValue=unescape(this.data[p][id_]);
                                    }catch(e){}
                                }
                                
                            }else{
                                if(this.data[this.id])
                                    this.currentValue= unescape(this.data[this.id]);
                            }
                        
                        }
                    }else{
                        
                        let data={};
                        data.id=this.id;
                        data.val=this.value;
                        data.index=this.index;
                        this.$emit('input',JSON.stringify(data));
                    }
                }else{
                     this.currentValue=unescape(this.data[this.index].vals)
                }
                
                if(this.currentValue==undefined||this.currentValue=="undefined"){
                    this.currentValue="";
                }
                if(this.first=='Y'){
                    this.currentValueOld=this.currentValue;
                    this.first='N';
                }
                
            }
        },
        watch:{
            currentValue: function (val) { //currentValue值变动就向父组件传值
                if(!val){
                    val="";
                }
                if(this.currentValueOld!=this.currentValue){
                    if(this.isSearch!="Y"){
                        let data={};
                        data.id=this.id;
                        data.val=val;
                        data.index=this.index;
                        //this.$emit('input',JSON.stringify(data));
                        //this.$emit('on-change',JSON.stringify(data));
                        if(this.data){
                            if(this.pId){
                            //  console.log("this.index======"+this.index);
                                if(this.index!=""&&(this.index==0||this.index>0)){
                                    let data_=this.data[this.pId][this.index];
                                    data_[this.cId]=val;
                                    if(this.extype=="amt"){
                                        let exkeys=this.exkey.split(",");
                                        let val_=Number(val);
                                        let val2_=Number(this.data[this.pId][this.index][exkeys[0]]);
                                        data_[exkeys[1]]=val_*val2_;
                                    }
                                    
                                    this.$set(this.data[this.pId],this.index,data_)
                                }else{
                                // console.log("ciDD=D=D=D=D=D=="+this.pId+"=="+this.cId+"="+val);
                                    this.$set(this.data[this.pId],this.cId,val);//给字段赋值
                                    if(this.extype=="amt"){
                                        let exkeys=this.exkey.split(",");
                                        let val_=Number(val);
                                        let val2_=Number(this.data[this.pId][this.index][exkeys[0]]);
                                        this.$set(this.data[this.pId],exkeys[1],val2_);
                                    }
                                    //该方法用于一个字段是其他好几个字段分值相加的结果
                                    //exkey里面就是所有需要计算的字段，用逗号分隔，最后的一个字段，就是需要赋值的字段
                                    if(this.extype=="total_amt"){
                                        let exkeys=this.exkey.split(",");
                                        let id_=exkeys[exkeys.length-1];
                                        let total_=0;
                                        for(let i=0;i<exkeys.length-1;i++){
                                            let val_=Number(this.data[this.pId][exkeys[i]]);
                                            total_=total_+val_;
                                        }
                                        this.$set(this.data[this.pId],id_,total_);
                                    }
                                }
                                
                            }else{
                                if(this.index!=""&&(this.index==0||this.index>0)){
                                    this.$set(this.data[this.index],this.id,val)
                                }else
                                    this.$set(this.data,this.id,val)
                            }
                        }else{
                            
                        }
                        
                    }else{
                        
                        this.$set(this.data[this.index],"vals",val);
                    }
                    
                    this.currentValueOld=val;

                }
              //  console.log("this.exkey======="+this.extype+"=="+this.exkey);
                
            },
            value:function (val) { //对值进行验证0
                
            },
            data:{
                handler: function (newVal) {
                   // console.log("data===================================================")
                    this.setData()
                },      
                deep: true    //深度监听
            }
        }
    }
</script>
<style scoped>
    .el-input__inner{
        height:25px;
    }
    input{
        height:25px;
    }
</style>